package com.example.f8399.testdemo.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.support.annotation.NonNull;
import android.support.v7.widget.AppCompatCheckBox;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.example.f8399.testdemo.R;
import com.example.f8399.testdemo.bean.SingleEntity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description recyclerView的适配器以及显示.
 *
 * @author lcfeng
 * @date   2018/8/24 16:35.
**/
public class RcAdapter extends RecyclerView.Adapter<BindingViewHolder> {

    private Context mContext;
    private List<SingleEntity> mList = new ArrayList<>();
    private LayoutInflater mLayoutInflater;
    private OnItemClickListener mListener;


    private Map<Integer,Boolean> map=new HashMap<>();// 存放已被选中的CheckBox


    public interface OnItemClickListener{
        void onItemClick(SingleEntity item);
    }
    public RcAdapter(Context context) {
        mContext = context;
        mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public RcAdapter(Context context, List<SingleEntity> list) {
        mContext = context;
        mList = list;
        mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

    @NonNull
    @Override
    public BindingViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ViewDataBinding binding = DataBindingUtil.inflate(mLayoutInflater, R.layout.item_rc_adapter, parent, false);
        return new BindingViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull final BindingViewHolder holder, int position) {
        SingleEntity entity = mList.get(holder.getAdapterPosition());
        ViewDataBinding binding = holder.getBinding();
        View root = binding.getRoot();
        AppCompatCheckBox cb_check = root.findViewById(R.id.cb_check);
        TextView tv_1 = root.findViewById(R.id.tv_1);

        tv_1.setText(entity.getName());

        //根据数据的状态来显示，不用再次判断if else,因为每次都会设置true或false的
        cb_check.setChecked(entity.isCheck());

        cb_check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                int position1 = holder.getLayoutPosition();
                /**
                 * 这句话一定要写在判断isChecked的前面，否则当你再次手动全部选择了之后，
                 * 文案不会改变的,已经吃了一次亏了，这是第二次了
                 * */
                mList.get(position1).setCheck(isChecked);

                if (isChecked) {
                    map.put(position1, true);
                    int page=0;
                    for(int i=0;i<mList.size();i++){
                        if(!mList.get(i).isCheck()){
                            ++page;
                        }
                    }
                    if(page==0){
                        if (mOnSetAllListener != null) {
                            mOnSetAllListener.setAll(true);
                        }
                    }
                }else {
                    map.remove(position1);
                    if (mOnSetAllListener != null) {
                        mOnSetAllListener.setAll(false);
                    }
                }

            }
        });


        root.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.onItemClick(mList.get(holder.getLayoutPosition()));
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public void setList(List<SingleEntity> list) {
        mList = list;
        notifyDataSetChanged();
    }

    /**
     * 点击事件
     * @param listener
     */
    public void setOnItemClickListener(OnItemClickListener listener) {
        mListener = listener;
    }

    public interface OnSetAllListener {
        void setAll(boolean status);
    }
    private OnSetAllListener mOnSetAllListener;

    public void setOnSetAllListener(OnSetAllListener onSetAllListener) {
        mOnSetAllListener = onSetAllListener;
    }

    public List<SingleEntity> getList() {
        return mList;
    }
}
